package com.zikao.common.service.impl;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.Query;

import com.zikao.common.dao.SysFileDao;
import com.zikao.common.entity.SysFileEntity;
import com.zikao.common.service.SysFileService;
import org.springframework.web.multipart.MultipartFile;


@Service("sysFileService")
@Data
public class SysFileServiceImpl extends ServiceImpl<SysFileDao, SysFileEntity> implements SysFileService {

    // 图片存放位置
    @Value("${sysFileService.imageRep}")
    private String imageRep;

    // 视频文件存放位置
    @Value("${sysFileService.videoRep}")
    private String videoRep;
    // 文件存放位置
    @Value("${sysFileService.fileRep}")
    private String fileRep;

    /**
     * 保存文件
     * @param file
     * @param uuid
     * @return
     */
    public boolean saveFile(MultipartFile file, String uuid){
        File path = getPath(file.getContentType());
        String filename = file.getOriginalFilename();
        SysFileEntity fileEntity = new SysFileEntity();
        fileEntity.setFileName(filename);
        fileEntity.setUuid(uuid);
        String storeAddress = path.getAbsolutePath();
        fileEntity.setStoreAddress(storeAddress);
        // 图片0,视频1,文件2
        if (file.getContentType().contains("image")){
            fileEntity.setFileType(0);
        }else{
            fileEntity.setFileType(2);
        }
        File saveFile = new File(path,uuid);
        try{
            baseMapper.insert(fileEntity);
            file.transferTo(saveFile);
            return true;
        } catch (IllegalStateException | IOException e){
            e.printStackTrace();
            return false;
        }
    }

    /***
     * 根据contentType获取存储路径
     * @param contentType
     * @return
     */
    private File getPath(String contentType){
        File imgPath=new File(imageRep);
        File filePath=new File(fileRep);
        if(!imgPath.isDirectory()) {
            imgPath.mkdir();
        }

        if(!filePath.isDirectory()) {
            filePath.mkdir();
        }
        if(contentType.contains("image")) {
            return imgPath;
        }else {
            return filePath;
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysFileEntity> page = this.page(
                new Query<SysFileEntity>().getPage(params),
                new QueryWrapper<SysFileEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String getFileAddr(String fileType) {
        if ("image".equals(fileType)){
            return imageRep;
        }else if ("video".equals(fileType)){
            return videoRep;
        }else if ("file".equals(fileType)){
            return fileRep;
        }else{
            return null;
        }
    }
}